package com.yoomiito.app.adapter.goods.list

import android.text.Spannable
import android.text.SpannableString
import android.text.style.RelativeSizeSpan
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.yoomiito.app.R
import com.yoomiito.app.base.App
import com.yoomiito.app.isEmptyOr0
import com.yoomiito.app.isNull
import com.yoomiito.app.model.gift.GiftGoods
import com.yoomiito.app.model.home.FeaturedGoodsInfo
import com.yoomiito.app.utils.AllUtils
import com.yoomiito.app.utils.ImageLoader
import com.yoomiito.app.utils.SpannableUtils
import com.yoomiito.app.utils.Tools
import com.zhy.view.flowlayout.FlowLayout
import com.zhy.view.flowlayout.TagAdapter
import com.zhy.view.flowlayout.TagFlowLayout

class NewYouPinAdapter(data: List<GiftGoods>?) : BaseQuickAdapter<GiftGoods, BaseViewHolder>(R.layout.youpin_list_goods, data) {

    override fun convert(helper: BaseViewHolder?, item: GiftGoods?) {
        ImageLoader.getInstance().into(mContext, item?.product_img, helper?.getView(R.id.item_gift_pic),R.drawable.goods_default)
        helper?.getView<TextView>(R.id.goodsTitle)?.text = item?.product_name
        helper?.setGone(R.id.item_coupon_3, false)

        //奖
        val awardView = helper?.getView<View>(R.id.item_gift)
        if (App.mAppConfig.isLogin) {
            //奖标记  粉丝以上显示奖
            awardView?.visibility = if (SpannableUtils.isEmptyOr0(item?.commission)) View.GONE else View.VISIBLE
            helper?.getView<TextView>(R.id.tv_bonus)?.text = "¥" + SpannableUtils.handlePrice(item?.commission)
        } else {
            awardView?.visibility = View.GONE
        }

        //
        val point = arrayListOf<String>()
        if(!(item?.product_point?.isNullOrEmpty()!!)){
            point.add(item.product_point)
        }
        helper?.getView<TagFlowLayout>(R.id.item_gift_flowLayout)?.adapter = object :TagAdapter<String>(point){
            override fun getView(parent: FlowLayout, position: Int, t: String): View {
                val tv = LayoutInflater.from(mContext).inflate(R.layout.item_goods_flag, parent, false) as TextView
                val lp = tv.layoutParams as ViewGroup.MarginLayoutParams
                lp.leftMargin = 0
                tv.text = if (t.length > 8) t?.substring(0, 8) + "..." else t
                return tv
            }
        }

        //价格
        val priceTv = helper?.getView<TextView>(R.id.item_gift_goods_price)
        val price: String
        if (!App.mAppConfig.isLogin||SpannableUtils.isEmptyOr0(item.user_deduction_account)||SpannableUtils.isEmptyOr0(item.deduction_account)) {//优米为0不显示
            price = SpannableUtils.handlePrice(item.sale_price)
            priceTv?.text = price
        } else {
            price = "${SpannableUtils.handlePrice(item.sale_price)} 可抵扣¥${item.deduction_account}"
            val addFlag = price.indexOf(" ")
            val spanText = SpannableString(price)
            spanText.setSpan(RelativeSizeSpan(0.65f), addFlag, price.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
            priceTv?.text = spanText
        }

        val oldPriceTv = helper?.getView<TextView>(R.id.item_gift_goods_old_price)
        oldPriceTv?.text = SpannableUtils.getSpannable("¥${item.market_price}",0)
        oldPriceTv?.visibility = if (item.market_price.isEmptyOr0()) View.GONE else View.VISIBLE
        //已售
        helper?.getView<TextView>(R.id.item_gift_goods_sale_size)?.text = "已售" + SpannableUtils.saleNum(item.volume.toDouble())
    }
}